﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace IntelligentDecisioning.SharePoint.Definition.Fields
{
    public class ChoiceField: Field
    {
        #region Constants



        #endregion

        #region Properties

        public List<String> Choices { get; set; }
        public String Format { get; set; }
        public bool AllowFillInChoice { get; set; }

        #endregion

        #region Constructor

        public ChoiceField()
            :base()
        {
            base.Type = SPFieldType.Choice.ToString();
            Choices = new List<string>();
            Format = "Dropdown";
        }

        public ChoiceField(String id, String name, String displayName, String type, String groupName, string staticName, List<string> choices, String format)
            :base(id,name,displayName,type,groupName,staticName)
        {
            Choices = choices;
            Format = format;
        }

        #endregion

        #region Custom Provisioning CAML

        protected override void AddCustomFieldAttributes(System.Xml.XmlWriter xmlWriter)
        {
            // Write the choice element specific CAML
            if (Choices.Count > 0)
            {

                xmlWriter.WriteAttributeString(DefinitionConstants.FILLINCHOICE_ATTRIBUTE, AllowFillInChoice.ToString());
                xmlWriter.WriteAttributeString(DefinitionConstants.FORMAT_ATTRIBUTE, Format.ToString());
                // source attribute required ?

                xmlWriter.WriteStartElement(DefinitionConstants.CHOICES_ELEMENT);

                foreach (String choice in Choices)
                {
                    xmlWriter.WriteElementString(DefinitionConstants.CHOICE_ELEMENT, choice);
                }

                xmlWriter.WriteEndElement();
            }
        }

        #endregion
    }
}
